﻿using System;
using System.Collections.Generic;
using Flurl;
using ZilLion.Core.ConfigManager.Manager;
using ZilLion.Core.Unities.UnitiesMethods;
using ZilLion.TaskManager.Entity;
using ZilLion.TaskManager.Entity.Config;
using ZilLion.TaskManager.Entity.Entity;
using ZilLion.TaskManager.Entity.Response;

namespace ZilLion.TaskManager.Repository
{
    public class TokenResponse : ResponseBase
    {
        public DateTime CreatedTime { get; set; }

        public string Token { get; set; }

        public DateTime NextExpTime { get; set; }
    }

    public class TaskManagerReponsitoryBase
    {
        private static string _cacheToken = string.Empty;
        private readonly object lockobj = new object();

        protected string GetJwt()
        {
            if (_cacheToken.IsNotNullOrEmpty())
                return _cacheToken;


            var url = Url.Combine(TaskManagerGlobalSetting.GetInstance().ManagerServiceHost, "Api",
                "TaskPlatform",
                "Jwt", "GetJwt", "TaskPlatform"
            );

            var tokenResponse = HttpHelper.HttpRequestPost<Dictionary<string, string>, TokenResponse>(url,
                new Dictionary<string, string>());


            lock (lockobj)
            {
                _cacheToken = tokenResponse?.Token ?? string.Empty;
            }

            return _cacheToken;
        }
    }
}